import { _decorator, Color, Component, find, Graphics, instantiate, Node, Prefab, v3, Vec2, Vec3 } from 'cc';
import ResManager from './ResManager';
import { PrefabEnum } from './PrefabEnum';
import WorldManager from './WorldManager';
import { UserInfo } from './UserInfo';
import { WorldController } from './WorldController';
const { ccclass, property } = _decorator;

@ccclass('LineGraphics')
export class LineGraphics extends Component {
    @property(Graphics)
    g: Graphics
    /**起点坐标 */
    startPos: Vec3
    /**终点坐标 */
    endPos: Vec3
    /**虚线得单位长度 */
    unitLength: number = 20
    /**虚线得单位个数 */
    count: number = 0
    /**偏移量 */
    offset: number = 0
    /**结束时间，过了这个时间，就要被移除 */
    endTime:number = 0

    start() {
       
    }

    update(deltaTime: number) {

        if(this.endTime > 0){
            let now = new Date().getTime() / 1000
            let time = this.endTime - (UserInfo.instance.offsetTime / 1000)
            if(time < now -2){
                this.node.destroy()
                UserInfo.instance.refreshVillageDetail()
                
            }
        }

        if (this.startPos && this.endPos) {
            this.offset += 100 * deltaTime
            this.renderDashed(this.startPos.clone(), this.endPos.clone(), this.offset % (this.unitLength * 2))
        }

    }

    /**
     * 根据世界坐标的两个点，绘制虚线
     * @param start 起始点，基于世界坐标
     * @param end 终点，基于世界坐标
     * @param endTime 结束时间，到了结束时间，就会被清除
     */
    static async createDashed(start: Vec3, end: Vec3, endTime:number = 0, color:Color = new Color('#E64242')):Promise<Node> {
        
        let line = await ResManager.instance.loadAssetByUrl<Prefab>(PrefabEnum.LineGraphics)
        let node = instantiate(line)
        find('Canvas').addChild(node)
        let com = node.getComponent(LineGraphics)
        com.drawDashed(start, end, endTime, color)
        
        return node
    }

   

    /**
     * 设置虚线的起点和终点，并开始绘制
     * @param start 起始点，基于世界坐标
     * @param end 终点，基于世界坐标
     */
    drawDashed(start: Vec3, end: Vec3, endTime:number, color:Color = new Color('#E64242')) {
        this.startPos = start
        this.endPos = end
        this.endTime = endTime
        this.g.fillColor = color
        this.g.strokeColor = color

        // this.g.fillColor = new Color('#25AC03')
        // this.g.strokeColor = new Color('#25AC03')
    }

    /**
     * 每一帧中，绘制一次虚线
     * @param start 起始点，基于世界坐标
     * @param end 终点，基于世界坐标
     * @param offset 偏移量
     */
    private renderDashed(start: Vec3, end: Vec3, offset: number = 0) {

        this.node.inverseTransformPoint(start, start)
        this.node.inverseTransformPoint(end, end)

        let dir = end.clone().subtract(start).normalize()
        let a = start.clone()
        a.add(dir.clone().multiplyScalar(offset))

        let b = v3(0, 0, 0)

        this.g.clear()
        this.count = 0
        while (true) {
            this.count++
            b = a.clone().add(dir.clone().multiplyScalar(this.unitLength))

            if (Vec3.distance(b, end) < this.unitLength) {
                break
            }

            if (this.count % 2 == 0) {
                this.g.moveTo(a.x, a.y);
                this.g.lineTo(b.x, b.y);
                this.g.stroke();
            }


            a = b.clone()
        }


    }
}


